package se480.paf.util;

import java.util.Comparator;
import java.util.Map;

/**
 * 
 * Modified from original by:
 * http://stackoverflow.com/questions/109383/how-to-sort-a-mapkey-value-on-the-values-in-java
 *
 */
public class ValueComparator implements Comparator<String> {

    Map<String, Integer> base;
    public ValueComparator(Map<String, Integer> base) {
        this.base = base;
    }

    // Note: this comparator imposes orderings that are inconsistent with equals.    
    public int compare(String a, String b) {
        if (base.get(a) > base.get(b)) {
            return -1;
        } 
        else if (base.get(a) < base.get(b)) {
            return 1;
        } // returning 0 would merge keys
        else {
        	// return based on word comparison
        	return a.compareTo(b);
        }
    }
}
